#include "SchedService.h"
#include <cstdio>

std::map<int, struct node> SchedService::getNodesInfo()
{
	char mess[SCHED_MESS_LEN];
	int i, num;
	struct node tmp;
	std::map<int, struct node> result;
	read(STDIN_FILENO, mess, SCHED_MESS_LEN);
	sscanf(mess, "total node %d", &num);
	for (i=0;i<num;i++) {
		read(STDIN_FILENO, mess, SCHED_MESS_LEN);
		sscanf(mess, "%d %lf %d %lf",
				&tmp.CPUNum,
				&tmp.CPUPower,
				&tmp.GPUNum,
				&tmp.GPUPower);
		result[i] = tmp;
	}
	return result;
}

std::string SchedService::getInputPath()
{
	char mess[SCHED_MESS_LEN];
	std::string tmp;
	read(STDIN_FILENO, mess, SCHED_MESS_LEN);
	tmp = mess;
	return tmp;
}

void SchedService::uploadKeyValue(std::vector<struct keyValue> schedInfo)
{
	char mess[SCHED_MESS_LEN];
	int i;
	sprintf(mess, "total %d", (int) schedInfo.size());
	write(STDOUT_FILENO, mess, SCHED_MESS_LEN);
	for (i=0;i<(int) schedInfo.size();i++) {
		sprintf(mess, "node %d %d %d",
				schedInfo[i].node,
				schedInfo[i].key,
				schedInfo[i].value);
		write(STDOUT_FILENO, mess, SCHED_MESS_LEN);
	}
	return;
}
